home *** CD-ROM | disk | FTP | other *** search
- /* SOURCE FILE: MATCH.C */
- /*****************************************************************************/
- /* match() verifies a data string, character by character, against a match */
- /* string. If the match string is shorter than the data string, the */
- /* last match-string character is used to verify the data string. */
- /* The match string may be composed of these match characters: */
- /* */
- /* A A-Z (uppercase letters) */
- /* # 0-9 only (numbers) */
- /* S 0-9 or + or - (signs or numbers) */
- /* F 0-9 or . (floating-point numbers) */
- /* L a-z, A-Z, 0-9 or ;:.,/?*-$#()'! (names, addresses) */
- /* and non-leading spaces */
- /* U A-Z, 0-9 or ;:.,/?*-$#()'! (uppercase names, addresses) */
- /* and non-leading spaces */
- /* P 0-9 or - or () or space (phone numbers) */
- /* Q one of "Yy1Nn0" (question reply) */
- /* X any printable character (text) */
- /* */
- /* Any other character in the match string is ignored. Spaces are matched */
- /* by L, U, and P. Empty match string matches any data. */
- /* Return value: YES = data string matches; NO = no match. */
- /*****************************************************************************/
-
- #include <stddefs.h>
- #include <ctype.h> /* for isxxx() character-type macro definitions */
-
- flag match(data_str, match_str)
- char data_str[]; /* source string to be verified */
- char match_str[]; /* match characters to match */
- {
- short ichar; /* input character counter */
- char c_data; /* character of data string */
- char c_match; /* character of match string */
-
- /* Initialize a variable to a function return. */
- short len_match = strlen(match_str);
- bflag matches = YES;
-
- for (ichar = 0; data_str[ichar] != '\0' && matches; ++ichar)
- {
-
- /* Determine match character. */
- if (ichar < len_match) /* Use next match character. */
- c_match = match_str[ichar];
- else if (len_match == 0) /* null match_str */
- c_match = '\0';
- else /* Use last character of match string. */
- c_match = match_str[len_match - 1];
- c_data = data_str[ichar];
- switch (c_match) /* Check match with data. */
- {
- case 'A': /* A-Z */
- if (!isupper(c_data))
- matches = NO;
- break;
- case '#': /* 0-9 only */
- if (!isdigit(c_data))
- matches = NO;
- break;
- case 'S': /* 0-9 or + or - only */
- if (!isdigit(c_data) && !strchr("+-", c_data))
- matches = NO;
- break;
- case 'L': /* a-z, A-Z, 0-9 or ;:.,/?*-$#()'! or space */
- if (!isalnum(c_data) && !strchr(";:.,/?*-$#()'! ",
- c_data))
- matches = NO;
- if (ichar == 0 && c_data == ' ') /* no leading blanks */
- matches = NO;
- break;
- case 'U': /* A-Z, 0-9 or ;:.,/?*-$#()'! or space */
- if (!isupper(c_data) && !isdigit(c_data) &&
- !strchr(";:.,/?*-$#()'! ", c_data))
- matches = NO;
- if (ichar == 0 && c_data == ' ') /* no leading blanks */
- matches = NO;
- break;
- case 'P': /* 0-9 or - or () or space */
- if (!isdigit(c_data) && !strchr("-() ", c_data))
- matches = NO;
- break;
- case 'F': /* 0-9 or . */
- if (!isdigit(c_data) && !strchr(".", c_data))
- matches = NO;
- break;
- case 'Q': /* Yy1Nn0 as reply to yes/no question */
- if (!strchr("Yy1Nn0", c_data))
- matches = NO;
- break;
- case 'X': /* Must be printable (' '-'~'). */
- if (!isprint(c_data))
- matches = NO;
- break;
- }
- }
- return (matches);
- }